package br.ufpe.cin.rva;

/************************************************************************\
 * This prototype was developed by Jader Abreu (jaderxnet@gmail.com /    *
 * jaderxnet.com) largely based on the code and tutorial found in:       *
 * http://jeffchien.com/2013/04/22/leap-motion-space-shooter-game/#comment-4
 * Copyright (C) 2012-2013 Leap Motion, Inc. All rights reserved.        *
 * Leap Motion proprietary and confidential. Not for distribution.       *
 * Use subject to the terms of the Leap Motion SDK Agreement available at*
 * https://developer.leapmotion.com/sdk_agreement, or another agreement  *
 * between Leap Motion and you, your company or other organization.      *
 \***********************************************************************/
 
import java.awt.Point;
import java.io.IOException;

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.SlickException;

import com.leapmotion.leap.Controller;
import com.leapmotion.leap.Finger;
import com.leapmotion.leap.FingerList;
import com.leapmotion.leap.Frame;
import com.leapmotion.leap.Hand;
import com.leapmotion.leap.Listener;
import com.leapmotion.leap.Vector;
 
class SampleListener extends Listener {
	public static float pontoToque = 160;
	Avoid myGame;
    AppGameContainer app;
 
    public SampleListener(Avoid a) throws SlickException {
        myGame = a;
        a.mouse = false;
        app = new AppGameContainer(myGame);
    }
 
    public void onInit(Controller controller) {
        System.out.println("Initialized");
    }
 
    public void onConnect(Controller controller) {
        System.out.println("Connected");
 
    }
 
    public void onDisconnect(Controller controller) {
        System.out.println("Disconnected");
    }
 
    public void onExit(Controller controller) {
        System.out.println("Exited");
    }
 
    public void onFrame(Controller controller) {
        // Get the most recent frame and report some basic information
        Frame frame = controller.frame();
 
        if (!frame.hands().empty()) {
            // Get the first hand
            Hand hand = frame.hands().get(0);
 
            // Check if the hand has any fingers
            FingerList fingers = hand.fingers();
            Vector avgPos;
            if (!fingers.empty()) {
                // Calculate the hand's average finger tip position
                myGame.fingersPositions =  new Point[fingers.count()];
                myGame.
                game.fingersZ = new int[fingers.count()];
                int i = 0;
                float x = 0, y = 0, z = 0;
                myGame.stopShootLaser();
                for (Finger finger : fingers) {
                    // add the position vectors up for each finger
                	avgPos = Vector.zero();
                    avgPos = avgPos.plus(finger.tipPosition());
                    // scale the x position to the game resolution
                    x = (((float) avgPos.getX()/2) + 100) * 5;
                    // z-axis value for the laser
                    y = (float) avgPos.getY();
                    myGame.game.fingersZ[i] = (int) y;
                    // scale the y position to the game resolution
                    z = (((float) avgPos.getZ()/2) + 100) * 6;
                    myGame.fingersPositions[i] = new Point((int)x, (int)z);
                    i++;
                	if (y <= pontoToque) {
                		myGame.shootLaser();
                	}
                }
                myGame.playSongs();
            }
        }
 
    }
}
 
class LeapControl {
    public static void main(String[] args) throws SlickException {
 
        // Create a sample listener and controller
        SampleListener listener = new SampleListener(
            new Avoid("Avoid Game", true, true));
        Controller controller = new Controller();
        // Have the sample listener receive events from the controller
        controller.addListener(listener);
        listener.app.setDisplayMode(800, 500, false);
        listener.app.setShowFPS(false);
        listener.app.start();
        // Keep this process running until Enter is pressed
        System.out.println("Press Enter to quit...");
        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // Remove the sample listener when done
        controller.removeListener(listener);
    }
}
